function AbsPopupBox() {
};

AbsPopupBox.gui = function(unique) {
  return { title: document.getElementById(unique + '_title_img'),
           menu: document.getElementById(unique + '_menu_div'),
           panel: document.getElementById(unique + '_panel_div'),
           close: document.getElementById(unique + '_close_button')
         };
};

function PopupBox(document, container, width, height, user, date, options) {
  this._doc = document;
  this._container = container;
  this._width = (width) ? width : '513';
  this._height = (height) ? height: '416';
  this._user = user;
  this._date = date;
  this._options = options;
  this._unique = PopupBox.unique();
  
  this._createElements();
  this._gui = AbsPopupBox.gui(this._unique);
}

PopupBox.prototype._createElements = function() {
  var table, tr, td, input;
  table = this._doc.createElement('table');
  table.style.width = this._width + 'px';
  table.style.height = this._height + 'px';
  table.border=0
  table.style.margin='20px 0 0 0'
  table.cellPadding = 0;
  table.cellSpacing = 0; 
  this._container.appendChild(table);
  
  tr=table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = '2px';
  td.style.width = '2px';
  td.style.backgroundImage='url(image/popupbox/tl.png)';
  td.style.backgroundRepeat = 'no-repeat';
  td = tr.insertCell(-1);
  td.style.height = '2px';
  td.style.width = (this._width-8)+'px';
  td.style.backgroundImage='url(image/popupbox/tm.png)';
  td.style.backgroundRepeat = 'repeat-x';
  td = tr.insertCell(-1);
  td.style.height = '2px';
  td.style.width = '6px';
  td.style.backgroundImage='url(image/popupbox/tr.png)';
  td.style.backgroundRepeat = 'no-repeat';
  
  tr=table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = (this._height-8) + 'px';
  td.style.width = '2px';
  td.style.backgroundImage='url(image/popupbox/lm.png)';
  td.style.backgroundRepeat = 'repeat-y';
  td = tr.insertCell(-1);
  td.style.height = (this._height-8) + 'px';
  td.style.width = (this._width-8) + 'px';
  td.style.background = '#f0f0f0 url(image/popupbox/back.png) repeat-x 0 0';
    
  var itable = this._doc.createElement('table');
  itable.style.width = (this._width-8) + 'px';
  itable.style.height = (this._height-8) + 'px';
  itable.cellPadding = 0;
  itable.cellSpacing = 0; 
  itable.border = 0
  itable.style.margin='-1px -3px 0 -1px';
  td.appendChild(itable);
  td = tr.insertCell(-1);
  td.style.height = (this._height-8) + 'px';
  td.style.width = '6px';
  td.style.textAlign = 'left'
  td.style.backgroundImage='url(image/popupbox/rm.png)';
  td.style.backgroundRepeat = 'repeat-y';
  
  tr=table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = '6px';
  td.style.width = '2px';
  td.style.backgroundImage='url(image/popupbox/bl.png)';
  td.style.backgroundRepeat = 'no-repeat';
  td = tr.insertCell(-1);
  td.style.height = '6px';
  td.style.width = (this._width-8) + 'px';
  td.style.backgroundImage='url(image/popupbox/bm.png)';
  td.style.backgroundRepeat = 'repeat-x';
  td = tr.insertCell(-1);
  td.style.height = '6px';
  td.style.width = '6px';
  td.style.backgroundImage='url(image/popupbox/br.png)';
  td.style.backgroundRepeat = 'no-repeat';


  itr = itable.insertRow(-1);
  itd = itr.insertCell(-1);
  itd.style.height = '29px';
  itd.style.width = (this._width-6) + 'px';
 // itd.align = 'center';
  itd.id=this._unique + '_menu_div';
  var table1 = this._doc.createElement('table');
  table1.style.width = (this._width-6) + 'px';
  table1.style.height = '29px';
  table1.cellPadding = 0;
  table1.cellSpacing = 0;
//  table1.border=1
  itd.appendChild(table1);
  
  tr1 = table1.insertRow(-1);
  td1 = tr1.insertCell(-1);
  td1.style.height = '29px';
  td1.style.width = '180px';
  td1.style.padding='0 0 0 11px'
  td1.style.whiteSpace = 'nowrap'
  td1.align = 'left';
  td1.id = this._unique + '_title_img';
  td1.style.fontFamily = 'Arial Black';
  td1.style.fontWeight = 'bold';
  td1.style.backgroundImage='url(image/popupbox/ttl.png)';
  td1.style.backgroundRepeat = 'no-repeat';
  
  td1 = tr1.insertCell(-1);
  td1.style.height = '29px';
  td1.style.width = (this._width - 240) + 'px';
  td1.style.backgroundImage='url(image/popupbox/ttm.png)';
  td1.style.backgroundRepeat = 'repeat-x';
  
  td1 = tr1.insertCell(-1);
  td1.style.height = '29px';
  td1.align = 'left';
  td1.style.verticalAlign='top';
  td1.style.textAlign = 'right';
  td1.style.paddingRight = '15px';
  td1.style.backgroundImage='url(image/popupbox/ttr.png)';
  td1.style.backgroundRepeat = 'no-repeat';
  var close = this._doc.createElement('img');
  close.style.cursor = 'pointer';
  close.src = 'image/popupbox/close.png';
  close.id = this._unique + '_close_button';
  td1.appendChild(close);
  
  
  itr=itable.insertRow(-1);
  itd = itr.insertCell(-1);
  itd.style.width = (this._width-8) + 'px';
  itd.style.height = (this._height-8-29) + 'px';
  itd.style.verticalAlign = 'top';
  itd.align = 'left';
  itd.id = this._unique + '_panel_div';
  itd.style.backgroundColor = '#ffffff';
};

PopupBox.unique = function() {
  var unique = 'u' + parseInt(new Date().getTime() * Math.random());
  unique = unique.replace('.', '');
  unique.substring(0, 16);
  return unique;
};

function ModulePopupBox() {
  this._background = null;
  this._container = null;
}

ModulePopupBox.prototype._reset = function(pos) {
  this._close();
  
  var size = this._xDocSize();
  this._background = document.createElement('div');
  this._background.className = 'popupSimpleWhite';
  this._background.style.width = size.w + 'px';
  this._background.style.height = size.h + 'px';
  document.body.appendChild(this._background);
  
  this._container = document.createElement('div');
  this._container.className = 'popupBase1';
  this._container.style.top = ((this._position[1] - 60) > 0) ? (this._position[1] - 60) + 'px' : '0px';
  this._container.style.left = ((this._position[0] + 200) > window.screen.width) ? (window.screen.width - 200) + 'px' : this._position[0] + 'px';
  this._background.appendChild(this._container);

  var posX = this._position[0];
  var posY = ((this._position[1] - 100) > 0) ? (this._position[1] - 100) : 0;
  //window.scrollTo(posX, posY);
};

ModulePopupBox.prototype._open = function(container, options) {
  var table, tr, td, _self = this;

  this._title = (options && options.t) ? options.t : '';
  
  this._position = (options && options.pos) ? options.pos : [((document.body.offsetWidth / 2) - 50), ((document.body.offsetHeight / 2) - 50)];
  
  this._reset();
  
  this._gui = new PopupBox(document, this._container, options.width, options.height, null, null, options)._gui;
  this._callbackFunc = (options && options.func) ? options.func : null;
  
  this._gui.title.appendChild(document.createTextNode(this._title));
  this._gui.close.onclick = function() { _self._close(); 
                                         if (_self._callbackFunc) {
                                           _self._callbackFunc();
                                         }
                                       };
  this._gui.panel.appendChild(container);
  
  this._moveX = 0;
  this._moveY = 0;
  this._moveTop = 0;
  this._moveLeft = 0;
  this._moveable = false;
  this._docMouseMoveEvent = document.onmousemove;
  this._docMouseUpEvent = document.onmouseup;
  this._iWidth = document.body.clientWidth;
  this._iHeight = document.body.clientHeight;
  this._width = (options && options.width) ? options.width : 513;

  this._gui.menu.onmouseover = function() { this.className = 'popupMove'; };
  this._gui.menu.onmousedown = function() { var evt = _self._getEvent();
                                            _self._moveable = true;
                                            _self._moveX = evt.clientX;
                                            _self._moveY = evt.clientY;
                                            _self._moveTop = parseInt(_self._container.style.top);
                                            _self._moveLeft = parseInt(_self._container.style.left);
                                          };
  this._gui.menu.onmousemove = function() { if (_self._moveable) {
                                              this.className = 'popupMove';
                                              var evt = _self._getEvent();
                                              var x = parseInt(_self._moveLeft) + parseInt(evt.clientX) - parseInt(_self._moveX);
                                              var y = parseInt(_self._moveTop) + parseInt(evt.clientY) - parseInt(_self._moveY);
                                              if ( x > 0 && ( x + _self._width < _self._iWidth) && y > 0 && (y + 300 < _self._iHeight) ) {
                                                _self._container.style.left = x + "px";
                                                _self._container.style.top = y + "px";
                                              } else {
                                                _self._container.style.left = x + "px";
                                                _self._container.style.top = y + "px";
                                              }
                                            }
                                          };
  this._gui.menu.onmouseup = function () { if (_self._moveable) {
                                             document.onmousemove = _self._docMouseMoveEvent;
                                             document.onmouseup = _self._docMouseUpEvent;
                                             _self._moveable = false;
                                             _self._moveX = 0;
                                             _self._moveY = 0;
                                             _self._moveTop = 0;
                                             _self._moveLeft = 0;
                                             this.className = 'popupUp';
                                           }
                                         };
  
  this._background.onmousemove = function() { if (_self._moveable) {
                                             var evt = _self._getEvent();
                                             var x = parseInt(_self._moveLeft) + parseInt(evt.clientX) - parseInt(_self._moveX);
                                             var y = parseInt(_self._moveTop) + parseInt(evt.clientY) - parseInt(_self._moveY);
                                             
                                             if ( x > 0 && ( x + _self._width < _self._iWidth) && y > 0 && (y + 300 < _self._iHeight) ) {
                                               _self._container.style.left = x + "px";
                                               _self._container.style.top = y + "px";
                                             } else {
                                               _self._container.style.left = x + "px";
                                               _self._container.style.top = y + "px";
                                             }
                                           }
                                         };
  
  this._background.onmouseup = function () { if (_self._moveable) {
                                            document.onmousemove = _self._docMouseMoveEvent;
                                            document.onmouseup = _self._docMouseUpEvent;
                                            _self._moveable = false;
                                            _self._moveX = 0;
                                            _self._moveY = 0;
                                            _self._moveTop = 0;
                                            _self._moveLeft = 0;
                                            this.className = 'popupUp';
                                          }
                                        };
};

ModulePopupBox.prototype._close = function() {
  if (this._background) {
    document.body.removeChild(this._background);
    this._background = null;
    this._container = null;
  }
};


ModulePopupBox.prototype._getEvent = function() {
  if(window.parent == window) {
    return window.event || arguments.callee.caller.arguments[0];
  } else {
    return window.parent.event || arguments.callee.caller.arguments[0];
  }
};

ModulePopupBox.prototype._xDocSize = function(document) {
    var b = document.body, e = document.documentElement;
    var esw = 0, eow = 0, bsw = 0, bow = 0, esh = 0, eoh = 0, bsh = 0, boh = 0;
    if (e) {
      esw = e.scrollWidth;
      eow = e.offsetWidth;
      esh = e.scrollHeight;
      eoh = e.offsetHeight;
    }
    if (b) {
      bsw = b.scrollWidth;
      bow = b.offsetWidth;
      bsh = b.scrollHeight;
      boh = b.offsetHeight;
    }
    return {
      w : Math.max(esw, eow, bsw, bow),
      h : Math.max(esh, eoh, bsh, boh)
    };
  };
  
  function popupMethod2LeavePage(method, page) {
	  var _self = this;
	  var div = document.createElement('div');
	  var pop = new ModulePopupBox();
	  var size = pop._xDocSize(parent.document);
	  var pos = [(size.w-600) / 2, (size.h-800) / 2];
	  pop._open(div, {
	    document: parent.document,
	    pos : pos,
	    width : 660,
	    height : 496
	  });

	  var table = document.createElement('table');
	  table.cellPadding=0;
	  table.cellSpacing=0;
	  table.style.width = '600px';
	  table.style.height = '496px';
	  div.appendChild(table);

	  var tr = table.insertRow(-1);
	  var td = tr.insertCell(-1);
	  td.style.textAlign = 'left';
	  td.style.verticalAlign = 'bottom';
	  td.style.height = '46px';
	  td.style.paddingBottom = '16px';
	  td.style.fontFamily = 'Trebuchet MS';
	  td.style.fontSize = '24px';
	  td.style.fontWeight = '400';
	  td.style.lineHeight = '20px';
	  td.style.color = '#fff';
	  td.appendChild(document.createTextNode('LEAVE PAGE'));

	  tr = table.insertRow(-1);
	  td = tr.insertCell(-1);
	  td.style.height = "30px";
	  td.style.borderTop = 'solid 1px #434343';

	  tr = table.insertRow(-1);
	  td = tr.insertCell(-1);
	  td.style.textAlign = 'left';
	  td.style.verticalAlign = 'top';
	  td.style.height = '45px';
	  td.style.fontFamily = 'Trebuchet MS';
	  td.style.fontSize = '13px';
	  td.style.fontWeight = '600';
	  td.style.color = '#bfbfbf';
	  td.innerHTML = '<span>Are you sure you want to navigate away form this page? \nPress <span style="color: #a7345e">��OK��</span> to continue, or <span style="color: #a7345e">��Cancel��</span> to stay on the current page.</span>';
	  

	  
	  tr = table.insertRow(-1);
	  td = tr.insertCell(-1);
	  td.align = "right";
	  td.style.verticalAlign = 'top';
	  td.style.height = '90px';

	  var itable = document.createElement('table');
	  itable.cellPadding=0;
	  itable.cellSpacing=0;
	  itable.style.marginTop = '25px';
	  td.appendChild(itable);

	  var itr = itable.insertRow(-1);
	  itd = itr.insertCell(-1);
	  var ok = document.createElement('img');
	  ok.src = 'ext/plugin/bin/<?php echo $folder; ?>/assets/ok.png';
	  ok.style.cursor = 'pointer';
	  ok.style.marginRight = '10px';
	  ok.onclick = function() {
	    pop._close();
	    var size = _xDocSize(parent.document);
	    var pos = [0, 0];
	    popupLoading(popup_loading, pos);
	    
	    _self._uploading = false;
	    
	    setUploadPageDiv(method, page);
	    setUploadContentDiv(method, page);
	  };
	  itd.appendChild(ok);
	  
	  itd = itr.insertCell(-1);
	  var cancel = document.createElement('img');
	  cancel.src = 'ext/plugin/bin/<?php echo $folder; ?>/assets/cancel.png';
	  cancel.style.cursor = 'pointer';
	  cancel.onclick = function() { 
	    pop._close(); 
	    _self._uploading = false;
	  };
	  itd.appendChild(cancel);
	  
	};
  
